let logger = require("pomelo-logger").getLogger(__filename);
let BaseTableService = require("../base/tableService");
let util = require('util');
let YuanBaoTableCreator = require("./zhajinhuaYuanBaoTable").create;
let PlayerCreator = require("./zhajinhuaPlayer").create;
let uuid = require('node-uuid');

/*开放的快速匹配场个数*/
const QUICK_JOIN_ROOM_NUM = 3;

let Service = function (app) {
    BaseTableService.call(this, app);
    this.gid = 10002;
};

util.inherits(Service, BaseTableService);

Service.prototype.getGid = function () {
    return this.gid;
};

Service.prototype.doCreateSystemTable = function () {
    let self = this;
    let rooms = this.app.get("rooms");
    for (let i = 0; i < QUICK_JOIN_ROOM_NUM && i < rooms.length; i++) {
        let room = rooms[i];
        for (let i = 0; i < 50; i++) {
            self.doCreateYuanBaoTable({
                tid: uuid.v4(),
                code: "0",
                room: room,
                maxPlayers: 5,
                minCompareRound: 1,
                compareMultiplier: 2,
                minSeeCardRound: 0,
                maxRounds: 15,
                visible: 1,
                creatorUid: 0,
                gid: 10002
            });
        }
    }
};

Service.prototype.setAllIn = function (frontendId, sessionId, value) {
    let table = this.findTableByPlayer(frontendId, sessionId);
    if (table == null) {
        return {resultCode: 404, message: "桌子不存在"};
    }
    let player = this.findPlayer(frontendId, sessionId);
    if (player == null) {
        return {resultCode: 404, message: "玩家不在线"};
    }
    if (!table.getIsPlaying()) {
        return {resultCode: 404, message: "游戏未开始"};
    }
    if (!player.isAlive()) {
        return {resultCode: 404, message: "已经失败了"};
    }
    table.setAllIn(player, value);
    return {resultCode: 0, message: "ok", value: player.getAllInValue()};
};

Service.prototype.compare = function (frontendId, sessionId, targetPlayerUid) {
    let zhajinhuaTableService = this.app.get("zhajinhuaTableService");
    let table = zhajinhuaTableService.findTableByPlayer(frontendId, sessionId);
    if (table == null) {
        return {resultCode: 404, message: "桌子不存在"};
    }
    let player = zhajinhuaTableService.findPlayer(frontendId, sessionId);
    if (player == null) {
        return {resultCode: 404, message: "玩家不在线"};
    }
    if (!table.getIsPlaying()) {
        return {resultCode: 404, message: "游戏未开始"};
    }
    if (!player.isAlive()) {
        return {resultCode: 404, message: "已经失败了"};
    }
    if (table.minSeeCardRound >= table.getNumOfRounds()) {
        return {resultCode: 404, message: "必闷" + table.minSeeCardRound + "轮"};
    }

    if (table.getActorUid() !== player.getUid()) {
        return {resultCode: 404, message: "还没有轮到您操作哦"};
    }

    let targetPlayer = table.findPlayerByUid(targetPlayerUid);
    if (!targetPlayer) {
        return {resultCode: 404, message: "目标玩家不存在"};
    }

    if (!targetPlayer.isAlive()) {
        return {resultCode: 404, message: "目标玩家已经结束游戏"};
    }
    if (table.minCompareRound >= table.getNumOfRounds()) {
        return {resultCode: 404, message: "最小比牌" + table.minCompareRound + "轮"};
    }

    let result = table.compare(player, targetPlayer);
    if (result) {
        return {resultCode: 0, message: "0k"};
    } else {
        return {resultCode: 404, message: "比牌失败"};
    }
};

Service.prototype.bet = function (frontendId, sessionId, amount) {
    let zhajinhuaTableService = this.app.get("zhajinhuaTableService");
    let table = zhajinhuaTableService.findTableByPlayer(frontendId, sessionId);
    if (table == null) {
        return { resultCode: 404, message: "桌子不存在"};
    }
    let player = zhajinhuaTableService.findPlayer(frontendId, sessionId);
    if (player == null) {
        return { resultCode: 404, message: "玩家不在线"};
    }
    if (!table.getIsPlaying()) {
        return { resultCode: 404, message: "游戏未开始"};
    }
    if (!player.isAlive()) {
        return { resultCode: 404, message: "已经失败了"};
    }
    if (table.minSeeCardRound >= table.getNumOfRounds() && (msg.amount > table.minBets)) {
        return { resultCode: 404, message: "必闷" + table.minSeeCardRound + "轮"};
    }

    if (table.getActorUid() !== player.getUid()) {
        return { resultCode: 404, message: "还没有轮到您操作哦"};
    }

    if (!amount) {
        return {resultCode: 404, message: "下注为空"};
    }

    if (player.getAllInValue() > 0 && amount > table.minBets) {
        return { resultCode: 404, message: "跟到底后只能跟住，不能加注"};
    }
    if (table.getNumOfRounds() > table.maxRounds) {
        return { resultCode: 404, message: "超过最大轮数！只能比牌或者放弃！"};
    }
    let result = table.bet(player, amount);
    if (result) {
        return { resultCode: 0, message: "ok"};
    } else {
        return { resultCode: 404, message: "您的余额不足，无法跟注了哦"};
    }
};

Service.prototype.doCreatePlayer = function (playerData, frontendId, sessionId) {
    return PlayerCreator(playerData, frontendId, sessionId);
};

Service.prototype.doValidateCreateTableParams = function (params) {
    if (params.maxRounds !== 1 && params.maxRounds !== 2 && params.maxRounds !== 3) {
        logger.error('invalid maxRounds = ' + params.maxRounds);
        return { resultCode: 404, message: "invalid maxRounds" };
    }
    if (params.minSeeCardRound !== 1 && params.minSeeCardRound !== 2 && params.minSeeCardRound !== 3) {
        logger.error('invalid minSeeCardRound = ' + params.maxRounds);
        return { resultCode: 404, message: "invalid minSeeCardRound"};
    }
    if (params.minCompareRound !== 1 && params.minCompareRound !== 2) {
        logger.error("invalid minCompareRound = " + params.minCompareRound);
        return { resultCode: 404, message: "invalid minCompareRound" };
    }
    if (params.compareMultiplier !== 1 && params.compareMultiplier !== 2) {
        logger.error("invalid compareMultiplier = " + params.minCompareRound);
        return { resultCode: 404, message: "invalid compareMultiplier"};
    }

    if (params.isVisible !== 1 && params.isVisible !== 2) {
        logger.error('invalid isVisible = ' + params.isVisible);
        return { resultCode: 404, message: "invalid isVisible" };
    }
    return {resultCode: 0, message: "ok"};
};

Service.prototype.fold = function (frontendId, sessionId) {
    let table = this.findTableByPlayer(frontendId, sessionId);
    if (table == null) {
        return { resultCode: 404, message: "桌子不存在"};
    }
    let player = this.findPlayer(frontendId, sessionId);
    if (player == null) {
        return { resultCode: 404, message: "玩家不在线"};
    }
    if (!table.getIsPlaying()) {
        return { resultCode: 404, message: "游戏未开始"};
    }
    if (!player.isAlive()) {
        return { resultCode: 404, message: "已经失败了"};
    }
    if (table.minSeeCardRound >= table.getNumOfRounds()) {
        return { resultCode: 404, message: "必闷" + table.minSeeCardRound + "轮"};
    }
    table.fold(player);
    return { resultCode: 0, message: "ok"};
};

Service.prototype.seeCard = function (frontendId, sessionId) {
    let table = this.findTableByPlayer(frontendId, sessionId);
    if (table == null) {
        return { resultCode: 404, message: "桌子不存在"};
    }
    let player = this.findPlayer(frontendId, sessionId);
    if (player == null) {
        return { resultCode: 404, message: "玩家不在线"};
    }
    if (!table.getIsPlaying()) {
        return { resultCode: 404, message: "游戏未开始"};
    }
    if (!player.isAlive()) {
        return { resultCode: 404, message: "已经失败了"};
    }
    if (table.minSeeCardRound >= table.getNumOfRounds()) {
        return { resultCode: 404, message: "必闷" + table.minSeeCardRound + "轮"};
    }
    table.seeCard(player);
    return { resultCode: 0, message: "ok", cards: player.getCards()};
};

Service.prototype.doCreateYuanBaoTable  = function (data) {
    let table = YuanBaoTableCreator(data);
    this.tables.push(table);
    return table;
};

Service.prototype.doPlayerReconnect = function (table, player) {
    player.push("onInitializeZhajinhuaTable", {table: table.toJSON(), myCards: player.getDisplayCards(table)});
};

module.exports.create = function (app) {
    return new Service(app);
};